package com.anti.modular.bus.busroomconfiguration.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.basicinformation.entity.BasicInformation;
import com.anti.modular.bus.basicinformation.param.BasicInformationParam;
import com.anti.modular.bus.busroomconfiguration.entity.BusRoomConfiguration;
import com.anti.modular.bus.busroomconfiguration.enums.BusRoomConfigurationExceptionEnum;
import com.anti.modular.bus.busroomconfiguration.param.BusRoomConfigurationParam;
import com.anti.modular.bus.busroomconfiguration.service.BusRoomConfigurationService;
import com.anti.modular.sys.dict.entity.SysDictData;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房间家私配置service接口实现类
 *
 * @author AnTi
 * @date 2021-04-07 21:09:17
 */
@Service
public class BusRoomConfigurationServiceImpl implements BusRoomConfigurationService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusRoomConfiguration getById(BusRoomConfigurationParam busRoomConfigurationParam){
        String sql="select * from bus_room_configuration where id =? ";
        return dbUtils.get(sql,BusRoomConfiguration.class,busRoomConfigurationParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BusRoomConfigurationParam busRoomConfigurationParam) {
        StringBuilder sb=new StringBuilder("select * from bus_room_configuration where 1=1 and status=0 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busRoomConfigurationParam)) {
            // 根据名称 查询
            if (ObjectUtil.isNotEmpty(busRoomConfigurationParam.getName())) {
               sb.append(" and name like ?");
               param.add("%"+busRoomConfigurationParam.getName()+"%");
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BusRoomConfiguration> list(BusRoomConfigurationParam busRoomConfigurationParam) {
        String sql="select * from bus_room_configuration where  status=0";
        return dbUtils.query(sql,BusRoomConfiguration.class);
    }

    @Override
    public void add(BusRoomConfigurationParam busRoomConfigurationParam) {
        checkParam(busRoomConfigurationParam, false);
        BusRoomConfiguration busRoomConfiguration = new BusRoomConfiguration();
        BeanUtil.copyProperties(busRoomConfigurationParam, busRoomConfiguration);
        busRoomConfiguration.setStatus(0);
        busRoomConfiguration.setCreateTime(new Date());
        busRoomConfiguration.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busRoomConfiguration);
    }


    public void delete(BusRoomConfigurationParam busRoomConfigurationParam) {
        BusRoomConfiguration busRoomConfiguration = this.queryBusRoomConfiguration(busRoomConfigurationParam);
        busRoomConfiguration.setUpdateTime(new Date());
        busRoomConfiguration.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        busRoomConfiguration.setStatus(2);
        dbUtils.update(busRoomConfiguration);
    }

    public void edit(BusRoomConfigurationParam busRoomConfigurationParam) {
        checkParam(busRoomConfigurationParam, true);
        BusRoomConfiguration busRoomConfiguration = this.queryBusRoomConfiguration(busRoomConfigurationParam);
        BeanUtil.copyProperties(busRoomConfigurationParam, busRoomConfiguration);
        busRoomConfiguration.setUpdateTime(new Date());
        busRoomConfiguration.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busRoomConfiguration);
    }

    @Override
    public BusRoomConfiguration detail(BusRoomConfigurationParam busRoomConfigurationParam) {
        return this.queryBusRoomConfiguration(busRoomConfigurationParam);
    }

    /**
     * 获取房间家私配置
     *
     * @author AnTi
     * @date 2021-04-07 21:09:17
     */
    private BusRoomConfiguration queryBusRoomConfiguration(BusRoomConfigurationParam busRoomConfigurationParam) {
        BusRoomConfiguration busRoomConfiguration = this.getById(busRoomConfigurationParam);
        if (ObjectUtil.isNull(busRoomConfiguration)) {
            throw new BusinessException(BusRoomConfigurationExceptionEnum.NOT_EXIST);
        }
        return busRoomConfiguration;
    }
    private void checkParam(BusRoomConfigurationParam busRoomConfigurationParam, boolean isExcludeSelf){
        Long id = busRoomConfigurationParam.getId();
        String name = busRoomConfigurationParam.getName();
        //构建带name和code的查询条件
        StringBuilder nameSb=new StringBuilder("select count(*) from bus_room_configuration where status =0 and name =?");
        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
        nameParams.add(name);
        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            nameSb.append(" and id <> ?");
            nameParams.add(id);
        }

        //查询重复记录的数量
        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new BusinessException(0,"家私配置已存在");
        }
    }

}
